package com.gdut.huayan.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdut.huayan.dao.LimitMoneyMapper;
import com.gdut.huayan.dao.MoneyStockMapper;
import com.gdut.huayan.dao.OrderDetailMapper;
import com.gdut.huayan.dao.StockMapper;
import com.gdut.huayan.entity.po.LimitMoney;
import com.gdut.huayan.entity.po.MoneyStock;
import com.gdut.huayan.entity.po.OrderDetail;
import com.gdut.huayan.entity.po.Stock;
import com.gdut.huayan.entity.vo.StockSaveVo;
import com.gdut.huayan.service.StockService;
import com.gdut.huayan.service.exception.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements StockService {

    @Autowired
    private LimitMoneyMapper limitMoneyMapper;

    @Autowired
    private MoneyStockMapper moneyStockMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Override
    public boolean haveActivity(Long activityId) {
        QueryWrapper<Stock> wrapper = new QueryWrapper<>();
        wrapper.eq("activity_id", activityId);
        return baseMapper.selectCount(wrapper) > 0;
    }

    @Override
    @Transactional
    public Long addLimitMoney(LimitMoney limitMoney) {
        limitMoneyMapper.insert(limitMoney);
        return limitMoney.getId();
    }

    @Override
    @Transactional
    public void deleteLimitMoney(Long id) {
        Wrapper<MoneyStock> wrapper = new QueryWrapper<MoneyStock>().eq("limit_money_id", id);
        if (moneyStockMapper.selectCount(wrapper) == 0) {
            limitMoneyMapper.deleteById(id);
        } else {
            throw new BaseException(400, "此价格标签正在使用，无法删除！");
        }
    }

    @Override
    @Transactional
    public void updateLimitMoney(LimitMoney limitMoney) {
        limitMoneyMapper.updateById(limitMoney);
    }

    @Override
    @Transactional
    public List<LimitMoney> getAllLimitMoney() {
        return limitMoneyMapper.selectList(null);
    }

    @Override
    public Long addStock(StockSaveVo stockSaveVo) {
        baseMapper.insert(stockSaveVo.getStock());
        if (stockSaveVo.getStock().getMoneyType() == 2) {
            for (MoneyStock moneyStock : stockSaveVo.getMoneyStocks()) {
                moneyStock.setStockId(stockSaveVo.getStock().getId());
                moneyStockMapper.insert(moneyStock);
            }
        }
        return stockSaveVo.getStock().getId();
    }

    @Override
    @Transactional
    public void deleteStock(Long id) {
        Wrapper<OrderDetail> wrapper = new QueryWrapper<OrderDetail>().eq("stock_id", id);
        if (orderDetailMapper.selectCount(wrapper) == 0) {
            baseMapper.deleteById(id);
        } else {
            throw new BaseException(400, "此存量标签已经有用户下单，无法删除！");
        }
    }

    @Override
    @Transactional
    public boolean updateStock(StockSaveVo stockSaveVo) {
        Wrapper<OrderDetail> wrapper = new QueryWrapper<OrderDetail>().eq("stock_id", stockSaveVo.getStock().getId());
        if (orderDetailMapper.selectCount(wrapper) == 0) {
            baseMapper.updateById(stockSaveVo.getStock());
            if (stockSaveVo.getStock().getMoneyType() == 2) {
                moneyStockMapper.deleteByMap(Map.of("stock_id", stockSaveVo.getStock().getId()));
                for (MoneyStock moneyStock : stockSaveVo.getMoneyStocks()) {
                    moneyStock.setStockId(stockSaveVo.getStock().getId());
                    moneyStockMapper.insert(moneyStock);
                }
            }
            return true;
        }
        throw new BaseException(4000,"已有用户参加该活动，无法修改");
    }


}
